package com.lin.issue.t.service;

import com.lin.issue.t.service.dao.DemoUserDao;
import com.lin.issue.t.service.entity.User;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author: linjiang
 * @date: 2025/7/21
 * 功能：测试spring事务的失效方式
 */
@Service
public class DemoT001ServiceImpl {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private DemoUserDao demoUserDao;

    @Transactional
    public void test01() throws SQLException, IOException {
        this.test01_002();
        User user = new User("linjiang");
        demoUserDao.insert(user);
        throw new RuntimeException("失败");
    }

    public void test01_002() throws SQLException {
        System.out.println("test02 exist transaction: " +
                TransactionSynchronizationManager.isActualTransactionActive()
        );

        jdbcTemplate.execute(
                "insert into sys_dept(name) values('java课程001')"
        );
    }

}
